import LXCOM from '../common/common.js'
import baseunitgrah from './baseunitgrah.js'
import { CoordTRFUtil } from '../common/tools/coordtool.js'
import Coord from '../common/struct/coord.js'
import ActPkg from './action/imgrectaction.js'
import GlobalConst from '../common/globalConst.js'
var global = GlobalConst.GlobalConst;
import {LGXEnums} from '../common/sysenum.js'
import ImageInfo from '../common/struct/basestruct/ImageInfo.js'

class ImageGraph extends baseunitgrah.BaseUnitGraph {
    constructor(gid) {
        super(gid);
        var p = new Coord.RectPos();
        this.setPos(p);

        var action = new ActPkg.ImageRecthAction(this);
        this.setActionSvc(action);
        this.m_image = null;
        this.m_graphType = LGXEnums.LGXGraphType.ImageGraph_type;
        this.m_width = 0;
        this.m_height = 0;
        this.m_scaleX = 1;
        this.m_scaleY = 1;
        this.m_imgURL = "/res/symbol2.jpg";
        this.m_base64Content = "";
    }

    display(map, mapInfo,forClip) {
        if (this.m_image == null) {
            super.displayGraphScope(map, mapInfo,true);
            return;
        }
        var scaling = mapInfo.getScaling();
        var pos = this.getPos();
        var x = pos.left;
        var y = pos.top;
        var leftTopPt = { x, y };
        var p1 = CoordTRFUtil.world2Screen(leftTopPt, mapInfo);

        var cntWorldPT = this.getZone().getCnt();
        var cntScreenPT = CoordTRFUtil.world2Screen(cntWorldPT, mapInfo);
        let rotv = this.getRadAngle();
        map.save();
        map.translate(cntScreenPT.x, cntScreenPT.y);
        map.rotate(-rotv);
        map.translate(-cntScreenPT.x, -cntScreenPT.y);

        let wid = this.m_width * this.m_scaleX * scaling;
        let hgt = this.m_height * this.m_scaleY * scaling;
        map.drawImage(this.m_image, 0, 0, this.m_width, this.m_height, p1.x, p1.y, wid, hgt);
        map.restore();

        super.displayGraphScope(map, mapInfo);
        this.displayFeaturePoint(map, mapInfo);
    }

    displayFeaturePoint(map, mapInfo) {
        if (!this._selected) {
            return;
        }
        super.displayFeaturePoint(map, mapInfo);
    }

    disInSymbol(transformInfo,map, mapInfo,forClip) {

    }

    distance(tPoint, scaling, mapscale) {
        if (!super.preCheckInScope(tPoint, scaling)) {
            return -1;
        }
        else {
            return 0;
        }
    }

    calculateSubKind(tPoint, scaling, mapscale) {
        if (!super.preCheckInScope(tPoint, scaling)) {
            return 0;
        }
        else {
            return 1;
        }
    }

    calScope(mapscale) {
        var rect = this.getPos().getZone();
        this._zone.clone(rect);
    }

    getLocalZone() {
        var rect = this.getPos().getZone();
        let cnt = rect.getCnt();
        rect.left = rect.left - cnt.x;
        rect.right = rect.right - cnt.x;
        rect.top = rect.top - cnt.y;
        rect.bottom = rect.bottom - cnt.y;
        return rect;
    }

    getFeaturePtLst() {
        var ptlst = new Array;
        var pos = this.getPos();
        let x = pos.left;
        let y = pos.top;
        let p1 = { x, y };
        ptlst.push(p1);

        x = pos.right;
        y = pos.top;
        let p2 = { x, y };
        ptlst.push(p2);

        x = pos.right;
        y = pos.bottom;
        let p3 = { x, y };
        ptlst.push(p3);

        x = pos.left;
        y = pos.bottom;
        let p4 = { x, y };
        ptlst.push(p4);
        return ptlst;
    }

    movePosOffset(tOffset, modCmdInfo) {
        if (this._selKind < 1) {
            return;
        }
        var pos = this.getPos()
        pos.offset(tOffset.x, tOffset.y);
    }

    getMainFeaturePTLst(tFeaturePTLst) {
        var posList = this.getFeaturePtLst();
        for (var i = 0; i < posList.length; i++) {
            var pt = posList[i];
            tFeaturePTLst.push({pt,kind:LGXEnums.AuxPointKind.end});
        }
    }

    assignInfo(info) {
        super.assignInfo(info);
        this.m_imgURL = info.imgURL;
        this.m_image = new Image();
        this.loadImageData(info.contentBs64);
    }

    loadImageData(imgContentBs64) {
        if (this.m_image && imgContentBs64 && imgContentBs64.length > 0) {
            this.m_base64Content = imgContentBs64;
            this.m_image.src = imgContentBs64;
            let that = this;
            this.m_image.onload = () => {
                that.m_width = that.m_image.width;
                that.m_height = that.m_image.height;
                var prePos = that.getPos();
                var left = prePos.left;
                var top = prePos.top;
                prePos.right = left + that.m_width;
                prePos.bottom = top - that.m_height;
                that.calScope(1);
            }
        }
    }
    modifyAttribute(info, mapscale) {
        if (this.m_image == null){
            this.m_image = new Image();
        }
        super.modifyCommonAttribute(info);
        this.loadImageData(info.contentBs64);
    }

    toInfo() {
        var info = new ImageInfo.ImageInfo();
        super.commoneProp2Info(info);
        let pos = this.getPos();
        info.x = pos.left;
        info.y = pos.top;
        info.pos.clone(pos);

        info.width = this.m_width;
        info.height = this.m_height;
        info.datasize = 0;
        info.scalex = this.m_scaleX;
        info.scaley = this.m_scaleY;
        info.symbolUnitIndex = 0;
        info.fileEx = "jpg";
        info.contentBs64 = this.m_base64Content;
        info.imgURL = this.m_imgURL;
        return info;
    }

    getPosCopy() {
        let pos = new Coord.RectPos();
        pos.clone(this._position);
        return pos;
    }

    setScaleX(s) { this.m_scaleX = s; }
    getScaleX() { return this.m_scaleX; }

    setScaleY(s) { this.m_scaleY = s; }
    getScaleY() { return this.m_scaleY; }

    //复制粘贴时，从复制源对象复制属性到本图形对应属性----虚函数，由实体继承类实现
    copySubPropFromBuf(bufInfo,tx,ty){
        this.copyCommnonPropFromBuf(bufInfo,tx,ty);
    }
}

export default { ImageGraph }
